package com.openlab.service.car.controller;

import com.openlab.common.utils.Result;
import com.openlab.common.utils.ResultCode;
import com.openlab.service.car.dto.UserBuildingDto;
import com.openlab.service.car.dto.UserCarDto;
import com.openlab.service.car.dto.WechatMpUserDto;
import com.openlab.service.car.service.UserBuildingService;
import com.openlab.service.car.service.UserCarService;
import com.openlab.service.car.service.WechatMpUserService;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author: chenglin
 */
@CrossOrigin // 允许跨域请求
@RestController
@RequestMapping("/car")
public class CarController {

    @Resource
    private UserBuildingService userBuildingService;

    @Resource
    private WechatMpUserService wechatMpUserService;

    @Resource
    private UserCarService userCarService;

    /**
     * 查询业主信息
     */
    @PostMapping("/owner")
    public Result findOwner(@RequestBody Map<String, Object> map) {
        Integer community_id = (Integer) map.get("community_id");
        String phone = (String) map.get("phone");


        WechatMpUserDto wechatMpUserDto = wechatMpUserService.findOwer(phone);
        if (ObjectUtils.isEmpty(wechatMpUserDto)) {
            return Result.ok(ResultCode.QUERY_ILLEFAL, "未查询到业主信息");
        }

        List<UserBuildingDto> userBuildingDtoList = userBuildingService.findBuilding(wechatMpUserDto.getId(), community_id);

        if (ObjectUtils.isEmpty(userBuildingDtoList)) {
            return Result.ok(ResultCode.QUERY_ILLEFAL, "未查询到业主房屋信息");
        }

        System.out.println(community_id + " " + phone);
        return Result.ok();
    }

    /**
     *
     */
    @PostMapping("/message")
    public Result insertCarMessage(@RequestBody UserCarDto userCarDto){

        List<UserCarDto> carMessage = userCarService.findCarMessage(userCarDto);

        if (!ObjectUtils.isEmpty(carMessage)) {
            return Result.ok(ResultCode.CAR_NUMBER_ALEADY_EXIST, "车辆信息已存在, 请手动回复绑定信息");
        }

        return Result.ok();




    }

}
